package com.vnp.cooking.adapter;

import java.util.ArrayList;

import org.apache.commons.net.bsd.RLoginClient;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.vnp.cooking.CacBuocThucHienActivity;
import com.vnp.cooking.DangCongThucActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.TaoMonAnNguyenLieuActivity;
import com.vnp.cooking.TungbuocthuchienActivity;
import com.vnp.cooking.UIcontrols.CustomImageView;
import com.vnp.cooking.UIcontrols.MyClickAbleImageView;
import com.vnp.cooking.UIcontrols.RounderImageView;
import com.vnp.cooking.bean.ObjBST;
import com.vnp.cooking.bean.ObjCachlam;
import com.vnp.cooking.utils.Utils;

public class ListCachlamAdapter extends ArrayAdapter<ObjCachlam> {
	/**
	 * 
	 */
	Context context;
	public static ArrayList<ObjCachlam> objCachlams;
	ListCachlamAdapter adapter = this;
	
	Dialog dialog;
	int positionItem;

	// thiet lap image loader
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565).build();

	public ListCachlamAdapter(Context context, ArrayList<ObjCachlam> objCachlams) {
		super(context, R.layout.list_cach_lam_item, objCachlams);
		this.context = context;
		ListCachlamAdapter.objCachlams = objCachlams;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public int getPosition(ObjCachlam item) {
		return super.getPosition(item);
	}

	@Override
	public ObjCachlam getItem(int position) {
		return null;
	}

	
	
	
	@Override
	public int getItemViewType(int position) {
		return super.getItemViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		return super.getViewTypeCount();
	}

	@Override
	public int getCount() {
		int size = 0;
		try {
			size = objCachlams.size() + 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return size;
		
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		Holder holder = null;
		LayoutInflater inflater = LayoutInflater.from(getContext());
		if (v == null) {
			v = inflater.inflate(R.layout.list_cach_lam_item, parent, false);

			holder = new Holder();

			holder.txtThutulam = (Button) v.findViewById(R.id.item_list_thu_tu_buoc_lam);
			holder.txtMotabuoclam = (TextView) v
					.findViewById(R.id.item_list_txt_mota_tungbuoc);
			holder.imgBuoclam = (RounderImageView) v
					.findViewById(R.id.item_list_img_tungbuoc);
			holder.btnEdit = (ImageButton) v
					.findViewById(R.id.btn_edit_congthuc_dadang);
			holder.frl = (ViewGroup) v.findViewById(R.id.frl);
			holder.fr = (ViewGroup) v.findViewById(R.id.fr);
			holder.rlDes = (ViewGroup) v.findViewById(R.id.rl_des);
			holder.frTo = (ViewGroup) v.findViewById(R.id.fr_to);
			// holder.txtMotabuoclam.setSelection(holder.txtMotabuoclam.getText().toString().length());
            v.setTag(holder);
			

		} else {
			holder = (Holder) v.getTag();
		}
		
		
		if (position == (objCachlams.size())) {
			// holder.imgBuoclam.setImageResource(R.drawable.ic_thembuocthuchien_03)
			// ;
			RounderImageView lastItem = new RounderImageView(context);
			/*lastItem.setScaleType(ImageView.ScaleType.FIT_XY);
			lastItem.setImageResource(R.drawable.ic_thembuocthuchien_03);*/
			Bitmap bmp;
			Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay(); 
			int width = display.getWidth();  // deprecated
			int height = (int) Utils.convertDpToPixel(250, context);  // deprecated
		    bmp=BitmapFactory.decodeResource(getContext().getResources(),R.drawable.ic_thembuocthuchien_03);//image is your image                                                            
		    bmp=Bitmap.createScaledBitmap(bmp, width/2,height, true);
//		    lastItem.setPadding(5, 5, 10, 10);
		    lastItem.setImageBitmap(bmp);
			lastItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent t = new Intent(context,
							TungbuocthuchienActivity.class);
					v.getContext().startActivity(t);
				}
			});
			return lastItem;
		}
			
			try {
				holder.objCachlam = objCachlams.get(position);
			} catch (Exception e) {
				e.printStackTrace();
			}
		
			// set gia tri 
			try {
				holder.txtThutulam.setText("" + objCachlams.get(position).getStep());
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				holder.txtMotabuoclam.setText(objCachlams.get(position).getDecriptionStep());
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				Log.i("DATA", "IMAGE: "+objCachlams.get(position).getUrlImageStep());
				if (!objCachlams.get(position).getUrlImageStep().toString().matches("")) {
					holder.frl.setBackgroundResource(R.drawable.shape);
					ImageLoader.getInstance().displayImage(
							objCachlams.get(position).getUrlImageStep().toString(),
							holder.imgBuoclam, options);
//					setDataChange(objCachlams);
				} else {
					Log.i("DATA", "Buoc nay ko co anh......");
					holder.frl.setVisibility(View.GONE);
//					setDataChange(objCachlams);
					RelativeLayout.LayoutParams Params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) Utils.convertDpToPixel(230, context));
					holder.txtMotabuoclam.setLayoutParams(Params1);
					holder.rlDes.setBackgroundResource(R.drawable.shape);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		
		/*holder.btnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialogReport(position);
			}
		});

		holder.fr.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent t = new Intent(context, TungbuocthuchienActivity.class);
				t.putExtra("objCachlam", objCachlams.get(position));
				getContext().startActivity(t);
			}
		});*/
			try {
				holder.btnEdit.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						showDialogReport(position);
					}
				});
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				holder.frTo.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent t = new Intent(context, TungbuocthuchienActivity.class);
						t.putExtra("objCachlam", objCachlams.get(position));
						getContext().startActivity(t);
					}
				});
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				holder.frTo.setLongClickable(true);
			} catch (Exception e) {
				e.printStackTrace();
			}

		/*if (position == (objCachlams.size() - 1)) {
			Animation anim = AnimationUtils.loadAnimation(getContext(),
					R.anim.gridview_fly_in_from_top_corner);
			v.setAnimation(anim);
			anim.start();
		} else {

		}*/

		return v;
	}

	// optimize
	public class Holder {
		Button txtThutulam;
		TextView txtMotabuoclam;
		RounderImageView imgBuoclam;
		ImageButton btnEdit;
		ViewGroup frl, fr, rlDes, frTo;
		public ObjCachlam objCachlam;
	}

	public void setDataChange(ArrayList<ObjCachlam> objCachlams) {
		if (ListCachlamAdapter.objCachlams != null)
			ListCachlamAdapter.objCachlams = null;
		ListCachlamAdapter.objCachlams = objCachlams;
		notifyDataSetChanged();
	}
	
	
	
	// show dialog chia se
		public void showDialogReport(final int positionItem) {
			dialog = new Dialog(context, R.style.DialogSlideAnim);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.dialog_edit_buoclam);

			// set cac phan tu ben trong
			Button btnXoabuoc = (Button) dialog.findViewById(R.id.btn_xoabuoc);
			Button btnChinhsua = (Button) dialog.findViewById(R.id.btn_suabuoclam);
			Button btnThoat = (Button) dialog.findViewById(R.id.btn_thoat);

			WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
			lp.copyFrom(dialog.getWindow().getAttributes());
			lp.width = WindowManager.LayoutParams.MATCH_PARENT;
			lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
			dialog.getWindow().setAttributes(lp);
			dialog.getWindow().setGravity(Gravity.BOTTOM);
			dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

			// setOnclick
			btnXoabuoc.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					dialog.dismiss();
					adapter.remove(objCachlams.get(positionItem));
					setDataChange(objCachlams);
				}
			});
			
			btnChinhsua.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					Intent t = new Intent(context, TungbuocthuchienActivity.class);
					t.putExtra("objCachlam", objCachlams.get(positionItem));
					getContext().startActivity(t);
				}
			});
			
			btnThoat.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
		}
	public void refillItems(ArrayList<ObjCachlam> objCachlams){
		objCachlams.clear();
		objCachlams.addAll(objCachlams);
		notifyDataSetChanged();
	}
	
	public ArrayList<ObjCachlam> getItems() {
		return objCachlams;
	}

	
}
